package dao

import (
	"gorm.io/gorm"
	"gowork/microSignalSMSSending/model"
	"time"
)

type SendingModeRecordDaoImf struct {

}

type SendingModeWeChat struct {
	Phone string
	WeChatCardUrl string
	WeChatNo string
	Content string
	CustomerId int64
	CustomerName string
	SendingModeRecordId int64
	Account string
}

func (sendingModeRecordDaoImf *SendingModeRecordDaoImf) GetSendingModeRecordByCustomerId(customerId int64, tx *gorm.DB) (int64 , error) {

	var count int64
	err := tx.Model(&model.SendingModeRecord{}).Where("customer_id = ?", customerId).Count(&count).Error
	if err != nil {
		return 0, err
	}

	return count, nil
}

func (sendingModeRecordDaoImf *SendingModeRecordDaoImf) GetSendingWeChatRecord(tx *gorm.DB) (smrList []*SendingModeWeChat, err error) {

	var sendOutTwoList []*SendingModeWeChat
	//tx.Joins("inner join call_log on sending_mode_record.customer_id = call_log.customer_id").
	//	Where("call_log.talk_time > ?", 30).
	//	Where("sending_mode_record.created_at < ?", "call_log.created_at").
	//	Where("send_out = ?", 2).
	//	Find(&sendOutTwoList)
	//smrList = append(smrList, sendOutTwoList...)

	var sendOutThreeList []*SendingModeWeChat
	//tx.Joins("inner join distribution_records on sending_mode_record.customer_id = distribution_records.customer_id").
	//	Where("distribution_records.allocate_time < ?", time.Now().AddDate(0 ,0 ,-3).Format("2006-01-02 15:04:05")).
	//	Where("send_out = ?", 3).
	//	Find(&sendOutThreeList)
	//smrList = append(smrList, sendOutThreeList...)


	err = tx.Raw("select c.phone, awci.we_chat_card_url, awci.we_chat_no, smr.content, c.id, c.name, smr.id, smr.account from sending_mode_record smr inner join call_log cl on smr.customer_id = cl.customer_id inner join adviser_we_chat_info awci on smr.account = awci.account inner join customer c on c.id = smr.customer_id where cl.talk_time > ? and smr.created_at < cl.created_at and smr.account = cl.account and smr.send_out = ? and smr.is_send = ?", 180, 2, 1).Scan(&sendOutTwoList).Error
	if err != nil {
		return nil, err
	}
	smrList = append(smrList, sendOutTwoList...)

	err = tx.Raw("select c.phone, awci.we_chat_card_url, awci.we_chat_no, smr.content, c.id, c.name, smr.id, smr.account from sending_mode_record smr inner join distribution_records dr on smr.customer_id = dr.customer_id inner join adviser_we_chat_info awci on smr.account = awci.account inner join customer c on c.id = smr.customer_id where dr.allocate_time < ? and smr.send_out = ? and smr.is_send = ?", time.Now().AddDate(0 ,0 ,-3).Format("2006-01-02 15:04:05"), 3, 1).Scan(&sendOutThreeList).Error
	if err != nil {
		return nil, err
	}
	smrList = append(smrList, sendOutThreeList...)

	return
}

func (sendingModeRecordDaoImf *SendingModeRecordDaoImf) UpdateIsSendById(Id int64, tx *gorm.DB) (map[string]interface{}, error) {

	err := tx.Model(&model.SendingModeRecord{}).Where("id = ?", Id).Update("is_send", 2).Error
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{"code":200, "message":"更新成功"}, nil
}
